<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="auth" uri="http://www.jiyinit.com/authTag"%>

<style>
    .page_box{
        position: relative;
        margin: 35px auto;
        padding: 30px 15px 0px;
        background-color: #fff;
        border-style: solid;
        border-color: #ddd;
        border-width: 1px;
        border-radius: 4px;
        box-shadow: none;
        width: 600px;
        min-width: 600px!important;
        height: auto;
    }
</style>
<div class="container-fluid">
    <div class="page_box">
        <form id="form" class="form-horizontal" onsubmit="return submitHandler()">
            <div class="form-group">
                <%--<label for="inputFilesPath" class="col-xs-4 control-label text-right">你的上线包主路径：</label>--%>
                <div class="col-xs-12" style="text-align: center;">
                    <h3>我的工作日志</h3>
                </div>
                <div class="col-xs-12">
                    <textarea id="content" class="form-control"
                           name="content" rows="9" placeholder="日志内容不要超过500字.."
                           onchange="checkContent(this.value)"
                           onkeydown="checkContent(this.value)"
                           onkeyup="checkContent(this.value)"
                    ></textarea>
                    <span style="float: right;"><span id="txtNum">0</span>/500</span>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-12" style="text-align: center;">
                    <button id="submit" type="submit" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript">
    var loadding;
    var title = "提示信息";
    var timeTask;

    var checkAdd = "${basePath}/workLogByPC/checkAdd";
    var addWorkLog = "${basePath}/workLogByPC/addWorkLog";

    timeTask = setInterval("checkContent()", 200);

    checkWorkLog();

    $("#submit").on("click", function () {
        disableBtns();
        checkContent();
        $.ajax({
            url: addWorkLog,
            type: "POST",
            data: $("#form").serialize(),
            dataType: "json",
            beforeSend: function (xhr) {
                return submitHandler();
            },
            success: function (data) {
                unDisableBtns();
                successInfo(data.data);
            },
            error: function () {
                unDisableBtns();
                errorInfo("系统异常");
            }
        })
        return false;
    });

    function submitHandler() {
        var content = $("#content").val();
        if(isNull(content)){
            $("#content").focus();
            loadding = layer.tips('日志内容不能为空!', '#content', {
                tips: [2, '#FF5722'],
                time: 2*1000
            });
            return false;
        }else{
            return true;
        }
    }

    function checkContent(obj) {
        var text = $("#content").val();
        if(text.length > 500){
            var str = text.substring(0, 500);
            $("#content").val(str);
            $("#txtNum").text(500);
        }else{
            $("#txtNum").text(text.length);
        }
    }

    function checkWorkLog(){
        $.ajax({
            url: checkAdd,
            type: "POST",
            data: {},
            dataType: "json",
            success: function (data) {
                if(data.status == "0"){
//                    var taskId = data.data.dailyLog;
//                    var alltors = data.data.alltors;
                    var workLog = data.data.message;
                    console.log(JSON.stringify(workLog));
                    if(isNotNull(workLog)){
                        var messageContent = data.data.message.messageContent;
                        var taskMessageId = data.data.taskMessageId;
                        if(isNotNull(messageContent)){
                            $("#content").text(messageContent);
                        }
                    }
                }
            },
            error: function () {
                errorInfo("系统异常");
            }
        })
    }

    /*是否为空*/
    function isNull(str) {
        return str == null || str == "";
    }
    /*是否不为空*/
    function isNotNull(str) {
        return !isNull(str);
    }
    /*禁用所有按钮*/
    function disableBtns() {
        $(".container :button").each(function (obj) {
            $(this).attr("disabled", "disabled");
        });
    }
    /*解开所有按钮*/
    function unDisableBtns() {
        $(".container :button").each(function (obj) {
            $(this).removeAttr("disabled");
        });
    }
    /*警告信息*/
    function alertInfo(info){
        layer.alert(info,{
            title: title,
            icon: 0,
            closeBtn: 0, //不显示关闭按钮
            anim: 4,//从左翻滚
            shadeClose: fasle, //开启遮罩关闭
        });
    }
    /*异常信息*/
    function errorInfo(info) {
        layer.alert(info,{
            title: title,
            icon: 2,
            closeBtn: 0, //不显示关闭按钮
            anim: 6,//抖动
            shadeClose: false, //开启遮罩关闭
        });
    }
    /*成功信息*/
    function successInfo(info) {
        layer.alert(info,{
            title: title,
            icon: 1,
            closeBtn: 0, //不显示关闭按钮
            anim: 0,
            shadeClose: false, //开启遮罩关闭
        });
    }

</script>
